'use strict';
const db = uniCloud.database()
const dbCmd = db.command
const formdb = db.collection('form')
const formitemdb = db.collection('form-item')
const formsubmitdb = db.collection('form-submit')
const uniID = require('uni-id')
exports.main = async (event, context) => {
	const uniIDIns = uniID.createInstance({ // 创建uni-id实例，其上方法同uniID
		context: context
	})
	var payload = await uniIDIns.checkToken(event.uniIdToken)
	// if (payload.code) {
	// 	return payload
	// }
	// console.log(payload)
	const uid = '' 
	if(!payload.code) uid = payload.uid

	let result = {
		code: 0,
		data: {}
	}

	//表单详情
	if (event.action === 'submit') {
	
		var param = event.param
		try {
			// let info = await formdb.doc(param.form_id).get()
			// if (info.result.data.length) { // 判断表单是否存在

				// if (info.result.data.islogin == 1 && payload.code) {
				// 	return payload
				// }

				formsubmitdb.add({
					form_id: param.form_id,
					info: param.items,
					user_id: uid,
					ispublic: 0,
					create_date: new Date().getTime()
				})
			// }
		} catch (e) {
			return e
			console.log(e)
		}
	}else if(events.action="detail")


	//返回数据给客户端
	return event
};
